require(['./config'],()=>{
    require(['header','footer'],()=>{  //依赖于header模块 header模块又将header.HTML中的文件给加载出来
        class login{
            constructor(){
                this.init().then(()=>{
                    this.login()
                    this.nameRegular()
                    this.renameRegular()
                })
                 this.clickRegister()
            
        }
        
        init(){
            return new Promise((resole,reject)=>{
                $('.box').show();
                $('.box2').hide();
                resole()
            })
            
        }

        //点击登录后 发送请求
        login(){
            $('.btn-login').on('click',()=>{
                console.log($('.username'),$('.pwd'))
                let name = $('.username').val();
                let pwd = $('.pwd').val();
                $.get({
                    url:'http://localhost/users%20ajax/api/login.php',
                    data:{
                        name,
                        pwd
                    }
                },resp=>{
                    resp = JSON.parse(resp)
                    if(resp.code == 200){
                        if($('.setCookie').prop('checked')){
                            this.setCookie("name",name,{expires:7,path:"/"})
                        }else{
                            this.setCookie("name",name,{path:"/"});
                        }
                        alert(resp.body.msg+"即将跳转到首页");
						location.href = "/index.html";
                    }else{
                        alert(resp.body.msg);
                    }
                })
            })
        }

        //点击注册后
        clickRegister(){
            //先将注册框显示
            $('.goregister').on('click',function(){
                // console.log($('.box'),$('.box2'))
                $('.box2').show();
                $('.box').hide();
            })

            //点击注册按钮
            $('.register').on('click',()=>{
                let name = $('.rgusername').val();
                let pwd = $('.rgpwd').val();
                //向php服务器那边发请求
                
                $.get({
                    url:'http://localhost/users%20ajax/api/register.php',
                    data:{name,pwd}
                },resp=>{
                    resp = JSON.parse(resp);
                    if(resp.code == 200){
                        alert(resp.body.msg+"去登陆");
                        location.href = "/html/login.html";
                    }
                })
            })
        }

        setCookie (key, value, option) {
            // 存的时候编码
            var str = `${key}=${encodeURIComponent(value)}`
            if (option) {
              if (option.path) {
                str += `;path=${option.path}`
              }
              if (option.expires) {
                // 有过期时间
                var date = new Date()
                date.setDate(date.getDate() + option.expires)
                str += `;expires=${date.toUTCString()}`
              }
            }
            document.cookie = str
        }

        //输入账号框正则表达式
        nameRegular(){
            $('.username').on('blur',function(){
                var myreg = /^1[3458]\d{9}$/;
                var myreg2 = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
                var inpVal = this.value;
                if (!myreg.exec(inpVal) && !myreg2.exec(inpVal)){
                    // alert('请输入正确的手机号')
                    $('.rg-p').remove();
                    $('<p class="rg-p">').html('请输入正确邮箱或手机号').appendTo($('.box .boxmin3'))
                }else{
                    $('.rg-p').remove();
                    $('<p class="rg-p">').html('格式正确').appendTo($('.box .boxmin3'))
                }
            })
        }

        //注册框正则表达式
        renameRegular(){
            $('.rgusername').on('blur',function(){
                var myreg = /^1[3458]\d{9}$/;
                var myreg2 = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
                var inpVal = this.value;
                if (!myreg.exec(inpVal) && !myreg2.exec(inpVal)){
                    // alert('请输入正确的手机号')
                    
                    $('.rg-p').remove();
                    $('<p class="rg-p">').html('请输入正确邮箱或手机号').appendTo($('.box2 .boxmin3'))
                }else{
                    $('.rg-p').remove();
                    $('<p class="rg-p">').html('格式正确').appendTo($('.box2 .boxmin3'))
                }
            })
        }

        }
        new login();
    })
})